import 'package:flutter_menu/data/obx_store.dart';
import 'package:objectbox/objectbox.dart';
import 'package:json_annotation/json_annotation.dart';

part 'category_model.g.dart';

@deprecated
@Entity()
@JsonSerializable()
class CategoryModel {
  @Id()
  int obxId;

  ///分类id
  @Index()
  String? id;

  ///分类名称
  String? name;

  ///分类第二名称
  String? secondName;

  ///排序字段
  int? sort;

  ///供应时间
  @JsonKey(fromJson: SupplyTime.toManyFromJson, toJson: SupplyTime.toManyJson)
  ToMany<SupplyTime> supplyTime = ToMany<SupplyTime>();

  CategoryModel({
    this.obxId = 0,
    this.id,
    this.name,
    this.secondName,
    this.sort = 0,
  });

  factory CategoryModel.fromJson(Map<String, dynamic> json) =>
      _$CategoryModelFromJson(json);

  Map<String, dynamic> toJson() => _$CategoryModelToJson(this);

  static ToOne<CategoryModel> toOneFromJson(Map<String, dynamic> json) =>
      ToOne(target: CategoryModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<CategoryModel> s) =>
      s.target != null ? _$CategoryModelToJson(s.target!) : {};

  static ToMany<CategoryModel> toManyFromJson(List<dynamic>? json) =>
      ToMany(items: json?.map((e) => CategoryModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<CategoryModel>? s) =>
      s?.toList().map((e) => e.toJson()).toList();
}

@Entity()
@JsonSerializable()
class SupplyTime with IEntity {
  @Id()
  int obxId;

  ///日期组合 1 - 7 代表着星期一至星期天
  List<int>? days;

  ///市别类型  0早市 1午市 2晚市 3夜市
  String? marketTypeCode;

  ///市别名称
  String? marketTypeName;

  @override
  int updateTime;

  SupplyTime({
    this.obxId = 0,
    this.updateTime = 0,
    this.days,
    this.marketTypeCode,
    this.marketTypeName,
  });

  factory SupplyTime.fromJson(Map<String, dynamic> json) =>
      _$SupplyTimeFromJson(json);

  Map<String, dynamic> toJson() => _$SupplyTimeToJson(this);

  static ToOne<SupplyTime> toOneFromJson(Map<String, dynamic> json) =>
      ToOne(target: SupplyTime.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<SupplyTime> s) =>
      s.target != null ? _$SupplyTimeToJson(s.target!) : {};

  static ToMany<SupplyTime> toManyFromJson(List<dynamic>? json) =>
      ToMany(items: json?.map((e) => SupplyTime.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<SupplyTime>? s) =>
      s?.toList().map((e) => e.toJson()).toList();

  @override
  String toString() {
    return 'SupplyTime{obxId: $obxId, days: $days, marketTypeCode: $marketTypeCode, marketTypeName: $marketTypeName, updateTime: $updateTime}';
  }

  @override
  int update(Store store, int? time) {
    updateTime = time ?? updateTime;
    return store.box<SupplyTime>().put(this);
  }
}
